const http = require('../../../utils/http.js');
const Net = require('../../../Net')
Page({
  data: {
    titleHeight: '',
    info: '',
    onUserWechat: false,
    statusd: 1,
    onShowStatus: false,
    payIcon: '0',
    onBalanceStatus: true
  },
  onTips() {
    wx.navigateTo({
      url: '/pages/onNewPages/TaskexchangeTisp/TaskexchangeTisp',
    })
  },
  onStatusIcon(e) {
    var _this = this
    _this.setData({
      payIcon: e.currentTarget.id
    })
    return false
    if (e.currentTarget.id == 0) {
      wx.showToast({
        title: '功能升级中...',
        icon: 'none'
      })
    } else {
      _this.setData({
        payIcon: e.currentTarget.id
      })
    }
  },
  onLoad: function (options) {
    var _this = this
    var query = wx.createSelectorQuery()
    query.select('#onPageView').boundingClientRect()
    query.select('#pageTitle').boundingClientRect()
    query.exec(function (res) {
      console.log(res)
      _this.setData({
        titleHeight: res[0].height - (res[1].height - 30)
      })
    })
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1000);
    _this.onPageInfo(options.type ? options.type : 1)
  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {},
  onPayButton() {
    var _this = this
    _this.setData({
      onShowStatus: !_this.data.onShowStatus
    })
  },
  onPageInfo(event) {
    var _this = this
    http.getRequest('v1/vip', {
        type: event,
        client_type:'3'
      },
      function (res) {
        console.log(res)
        _this.setData({
          info: res.data,
        })
      },
      function (err) {})
  },
  onGoIsVipPlus() {
    var _this = this,
      type = _this.data.info.goods_link.link.apply_name
      if(wx.getStorageSync('token')){
        if (type != 'vip') {
          wx.navigateTo({
            url: '/pages/onNewPages/selfSupport/selfSupport?id=' + this.data.info.goods.tao_id,
          })
        } else {
          var info = _this.data.info,
            money = info.balance
          info.balance = info.balance
          info.actual_amount = info.goods_link.link.total_fee
          if (Number(money) - Number(info.goods_link.link.total_fee) > 0) {
            _this.setData({
              onShowStatus: !_this.data.onShowStatus,
              payIcon: 1,
              info: info
            })
          } else {
            _this.setData({
              onShowStatus: !_this.data.onShowStatus,
              payIcon: 0,
              onBalanceStatus: false,
              info: info
            })
          }
        }
      }else{
        wx.navigateTo({
          url: '/pages/Login/Index/index',
        })
      }
  },
  onShowUserWechat() {
    var _this = this
    _this.setData({
      onUserWechat: !_this.data.onUserWechat
    })
  },
  onCapy(e) {
    var _this = this
    wx.setClipboardData({
      data: e.currentTarget.id,
      success(res) {
        wx.getClipboardData({
          success(res) {
            _this.setData({
              onUserWechat: false
            })
          }
        })
      }
    })
  },
  onButStatus(e) {
    var type = e.currentTarget.dataset.type,
      _this = this
    if (type == 'invite') {
      wx.navigateTo({
        url: '/pages/onNewPages/Taskexchange/Taskexchange',
      })
    }
  },
  /**
   * 开通超级创客
   */
  onPay(e) {
    wx.showToast({
      title: '开通中...',
      icon: 'loading'
    })
    Net.link('v1/activate', {
      total_fee: e.currentTarget.id,
      pay_type: e.currentTarget.dataset.type
    }, function (res) {
      console.log(res)
      if (e.currentTarget.dataset.type != 3) {
        Net.link('v1/callback', {
          type: res.data.data.type,
          order_num: res.data.data.order_num
        }, function (reponse) {
          console.log(reponse)
          if (reponse.data.code == 201 || reponse.data.msg == "开通成功") {
            wx.hideToast()
            wx.redirectTo({
              url: '/pages/onNewPages/onVipSuccess/onVipSuccess?msg=' + reponse.data.msg + '&message=' + reponse.data.data.message,
            })
          }
        })
      } else {
        wx.requestPayment({
          nonceStr: res.data.data.payment.nonceStr,
          package: res.data.data.payment.package,
          paySign: res.data.data.payment.paySign,
          timeStamp: res.data.data.payment.timeStamp,
          signType: 'MD5',
          success(rep) {
            wx.showToast({
              title: '支付成功',
              icon: 'success'
            })
            setTimeout(() => {
              Net.link('v1/callback', {
                type: res.data.data.type,
                order_num: res.data.data.order_num
              }, function (reponse) {
                console.log(reponse)
                if (reponse.data.code == 201 || reponse.data.msg == "开通成功") {
                  wx.hideToast()
                  wx.redirectTo({
                    url: '/pages/onNewPages/onVipSuccess/onVipSuccess?msg=' + reponse.data.msg + '&message=' + reponse.data.data.message,
                  })
                } else {
                  wx.showToast({
                    title: reponse.data.msg,
                    icon: 'none'
                  })
                }
              })
            }, 1000);
          },
          fail(res) {
            wx.showToast({
              title: '支付失败',
              image: 'https://huixuangou.oss-cn-beijing.aliyuncs.com/wechat/Xz_MiNi_image/icon_toast_defeat@2x.png',
            })
          }
        })
      }

    })
  },
})